package com.xx.jfb.server;

import cn.hutool.core.collection.ListUtil;

import cn.hutool.extra.emoji.EmojiUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xx.lesson.计算器;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;

public class NormalFactory implements Factory{
    @Override
    public String callback(XXRequest request) throws UnsupportedEncodingException {
        if("/getAll".equals(request.getUrl())){
            List<WanJiaZhanji> list =  readDB();
            return JSONUtil.toJsonStr(list);
        }
        if("/getByMingChen".equals(request.getUrl())){
            List<WanJiaZhanji> list =  readDB();
            WanJiaZhanji wanjia = null;
            for(WanJiaZhanji wj : list){
                String mingcheng = URLDecoder.decode(request.getParams().get("mingchen"), "UTF-8");
                if(wj.getMingchen().contains(mingcheng)){
                    wanjia = wj;
                    break;
                }
            }
            return JSONUtil.toJsonStr(ListUtil.toList(wanjia));
        }
        if("/zhanji".equals(request.getUrl())){
            List<WanJiaZhanji> list =  readDB();
            WanJiaZhanji wanjia = null;
            for(WanJiaZhanji wj : list){
                if(wj.getZhanji().equals(request.getParams().get("bf"))){
                    wanjia = wj;
                    break;
                }
            }
            return JSONUtil.toJsonStr(ListUtil.toList(wanjia));
        }

        if("/jisuanqi".equals(request.getUrl())){
            计算器 jsq = new 计算器();

            jsq.参数1 =Float.parseFloat(request.getParams().get("cs1"));
            jsq.参数2= Float.parseFloat(request.getParams().get("cs2"));
            jsq.符号 = request.getParams().get("fh");

            float 结果 = jsq.运算();
            return String.valueOf(结果);
        }
        return "你访问的地址不存在";
    }

    void zhanji(String bf){

    }



    public static List<WanJiaZhanji> readDB(){
        List<WanJiaZhanji> list = new ArrayList<>();
        String filePath = "zhanji.txt";
        try{
            FileInputStream fin = new FileInputStream(filePath);
            InputStreamReader reader = new InputStreamReader(fin);
            BufferedReader buffReader = new BufferedReader(reader);
            String strTmp = "";
            while((strTmp = buffReader.readLine())!=null){
                //  System.out.println("strTmp:" + strTmp);
                WanJiaZhanji wj = new  WanJiaZhanji();
                String [] array = strTmp.split(";");
                wj.setBangdan(array[0]);
                wj.setMingchen(array[1]);
                wj.setZhanji(array[2]);
                //  System.out.println("wj:" + array[0] + array[1] + array[2]);

                list.add(wj);
            }
            buffReader.close();
        }catch(Exception e){
            System.out.println("错误："+ e.getMessage());
        }
        // System.out.println("list size"+ list.size());
        return list;
    }

    //玩家战绩
    public static class WanJiaZhanji{
        //黑榜；红榜
        private String bangdan;
        //名称
        private String mingchen;
        //战绩
        private String zhanji;

        public void setBangdan(String bangdan) {
            this.bangdan = bangdan;
        }

        public String getBangdan() {
            return bangdan;
        }

        public void setMingchen(String mingchen) {
            this.mingchen = mingchen;
        }

        public String getMingchen() {
            return mingchen;
        }

        public void setZhanji(String zhanji) {
            this.zhanji = zhanji;
        }
        public String getZhanji() {
            return zhanji;
        }

        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return getBangdan() +"|" + getMingchen() +"|" + getZhanji();
        }
    }


}
